package com.example.gamblingshow;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;

import javax.swing.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Formatter;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HelloController implements Runnable {
    @FXML
    private Label playerNumText;//展示第几个玩家的标签
    @FXML
    private Label resultText;//展示结果的标签
    @FXML
    private Label label4;//数字4
    @FXML
    private Label label5;//数字5
    @FXML
    private Label label6;//数字6
    @FXML
    private Button startButton;//开始按钮
    @FXML
    private Label label1;//数字1
    @FXML
    private Label label2;//数字2
    @FXML
    private Label label3;//数字3
    @FXML
    private TextArea displayArea;//展示区域
    //随机数
    int[] randNums = new int[7];//生成6个随机数，表示投6个骰子

    Scanner in = new Scanner(System.in);

    //点击start后改变界面中的数字
    @FXML
    void mouseClicked(MouseEvent event) {
        if (myTurn) {
            for (int i = 0; i < 6; i++) {
                //randNums[i] = in.nextInt();//从命令行输入指定的点数
                randNums[i] = (int) (1 + Math.random() * (6 - 1 + 1));//得到六个范围是1-6的随机数
            }
            //label设置随机数结果
            label1.setText(String.valueOf(randNums[0]));
            label2.setText(String.valueOf(randNums[1]));
            label3.setText(String.valueOf(randNums[2]));
            label4.setText(String.valueOf(randNums[3]));
            label5.setText(String.valueOf(randNums[4]));
            label6.setText(String.valueOf(randNums[5]));

            //传到server端判断结果
            output.format("%d %d %d %d %d %d\n", randNums[0], randNums[1], randNums[2], randNums[3], randNums[4], randNums[5]);
            output.flush();
            myTurn = false;
        }
    }

    private Socket connection;
    private Scanner input;
    private Formatter output;

    private String myMark;//这位玩家的mark
    private boolean myTurn;//判断是否是自己的回合
    private final String STARTER = "X1";
    private String Host;

    @Override
    public void run() {
        myMark = input.nextLine();//从server接收传过来的mark
        SwingUtilities.invokeLater(() -> playerNumText.setText(myMark));

        myTurn = (myMark.equals(STARTER)); //判断是否是本client的步子

        while (true) {
            if (input.hasNextLine()) processMessage(input.nextLine());
        }
    }

    //处理server发送过来的消息
    private void processMessage(String message) {
        switch (message)
        {
            case "zhuangyuan" -> setResult("状元");
            case "zhuangyuan but no reward" -> setResult("no reward");
            case "bangyan" -> setResult("榜眼");
            case "bangyan but no reward" -> setResult("no reward");
            case "tanhua" -> setResult("探花");
            case "tanhua but no reward" -> setResult("no reward");
            case "jinshi" -> setResult("进士");
            case "jinshi but no reward" -> setResult("no reward");
            case "juren" -> setResult("举人");
            case "juren but no reward" -> setResult("no reward");
            case "xiucai" -> setResult("秀才");
            case "xiucai but no reward" -> setResult("no reward");
            case "nothing" -> setResult("nothing");
            case "myTurn" -> {
                myTurn=true;
                displayMessage("It's my turn!\n");
            }
            default -> displayMessage(String.format("%s\n", message));
        }
    }

    //展示结果
    private void setResult(String result) {
        Platform.runLater(() -> {
            switch (result) {
                case "nothing" -> {
                    resultText.setText("再接再厉！");
                    displayMessage("再接再厉\n");
                }
                case "no reward" -> {
                    resultText.setText("获奖但已无奖品");
                    displayMessage("Ah oh, no reward\n");
                }
                default -> {
                    String message = String.format("获得%s！\n", result);
                    resultText.setText(message);
                    displayMessage(message);
                }
            }
        });
    }

    //在展示区展示message
    private void displayMessage(final String messageToDisplay) {
        SwingUtilities.invokeLater(() -> displayArea.appendText(messageToDisplay));
    }

    public void initialize() {
        Host = "127.0.0.1";
        displayArea.setEditable(false);

        startClient();
    }

    //start the client thread
    public void startClient() {
        try {
            // make connection to server
            connection = new Socket(InetAddress.getByName(Host), 12345);
            // get streams for input and output
            input = new Scanner(connection.getInputStream());
            output = new Formatter(connection.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        ExecutorService worker = Executors.newFixedThreadPool(1);
        worker.execute(this);
    }
}
